The present invention relates to testing integrated circuits, either before or after they are inserted into chip packages. In particular, this application provides a method and algorithm for automatically inserting the necessary hardware test structures into any digital integrated circuit, in order to drastically reduce the cost of testing.